<!--电子围栏页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input @keydown.enter="loadGeo" v-model="geoSearchForm.name" placeholder="请输入" style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select @change="loadGeo" placeholder="请选择" v-model="geoSearchForm.status">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeo">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geoTableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true" width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center" >
        <template #default="scope" >
          <el-switch v-model="scope.row.status" @change="changeStatus(scope.row.id,scope.row.status)" inactive-value="0" active-value="1"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" @click="loadVehicle(scope.row.id)" link>管理车辆</el-button>
          <el-button type="primary" @click="removeGeofence(scope.row.id)" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" @click="showUnbindVehicleList" style="margin:10px 0;">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" :formatter="vehicleTypeFormatter" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button @click="removeBindVehicle(scope.row.id)" link type="primary">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible" :before-close="handleClose">
    <el-table :data="unBindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" :formatter="vehicleTypeFormatter"  align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" :formatter="vehicleStatusFormatter" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="bindNewVehicle(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
import router  from "@/router";

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);
//对车辆类型进行转换
const vehicleTypeFormatter=(row,column,cellValue,index)=>{
  if (cellValue==10){
    cellValue='轿车';
  }else if(cellValue==20){
    cellValue='货车';
  }else if(cellValue==30){
    cellValue='客车';
  }else if(cellValue==4){
    cellValue='挂车';
  }
  return cellValue;
}
//车辆状态显示内容进行转换
const vehicleStatusFormatter = (row,column,cellValue,index)=>{
  if (cellValue==1){
    cellValue = '空闲';
  }else if(cellValue==2){
    cellValue = '占用';
  }
  return cellValue;
}

// 查询条件
const geoSearchForm = ref({name:'',status:''})
// 电子围栏表格数据
const geoTableData = ref([]);
// 加载电子围栏的方法
const loadGeo=()=>{
  let data = qs.stringify(geoSearchForm.value);
  console.log(data)
  axios.get(BASE_URL+"/v1/geofence/select?"+data).then((response)=>{
    if (response.data.code==2000){
      geoTableData.value=response.data.data;
    }else{
      ElMessage.error(response.data.message);
    }
  })
}
// 重置方法
const resetSearch = ()=>{
  geoSearchForm.value={};
  loadGeo();
}

onMounted(()=>{
  loadGeo();
})

//当前电子围栏的id
var geoId;

// 围栏上的车辆信息
const bindVehicleArr = ref([]);
// 加载指定围栏上的车辆数据的信息
const loadVehicle=(geofenceId)=>{
  geoId=geofenceId;
  bindVehicleDialogVisible.value=true;
  axios.get(BASE_URL+"/v1/vehicle/select?geofenceId="+geofenceId).then((response)=>{
    if (response.data.code==2000){
      bindVehicleArr.value=response.data.data;
    }else{
      ElMessage.error(response.data.message);
    }
  })
}

//移出围栏绑定车辆  id->车辆id
const removeBindVehicle = (id)=>{
  axios.post(BASE_URL+"/v1/vehicle/unbind/"+id).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success("车辆解绑成功")
      loadVehicle(geoId);
    }else{
      ElMessage.error(response.data.message);
    }
  })
}
// 待绑定车辆数据
const unBindVehicleArr=ref([]);
//添加绑定车辆按钮
const showUnbindVehicleList=()=>{
  AddBindVehicleDialogVisible.value=true;
  axios.get(BASE_URL+"/v1/vehicle/select?geofenceBindStatus=0").then((response)=>{
    if (response.data.code==2000){
      unBindVehicleArr.value=response.data.data;
    }else{
      ElMessage.error(response.data.message);
    }
  })
}

//围栏绑定车辆
const bindNewVehicle = (vehicleId)=>{
  axios.post(BASE_URL+"/v1/vehicle/bind/"+geoId+"/"+vehicleId).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success("车辆绑定成功");
     showUnbindVehicleList();
    }else{
      ElMessage.error(response.data.message);
    }
  })
}
// 修改围栏状态
const changeStatus = (id,status)=>{
  let data = qs.stringify({id:id,status:status});
  axios.post(BASE_URL+"/v1/geofence/save",data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success("状态修改成功");
      loadGeo();
    }else{
      ElMessage.error(response.data.message);
    }
  })
}
// 删除围栏
const removeGeofence = (id)=>{
  axios.post(BASE_URL+"/v1/geofence/removeGeofence/"+id).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success("删除成功");
      loadGeo();
    }else{
      ElMessage.error("请先移除所有已绑定车辆");
    }
  })
}
//处理内层弹框关闭的方法
const handleClose=()=>{
  //重新加载当前围栏的所有已绑定的车辆
  loadVehicle(geoId);
  //再关闭内层 可绑定车辆列表的弹窗
  AddBindVehicleDialogVisible.value=false;
}
</script>

<style scoped>
</style>